//
//  ViewController.m
//  03-25-JSON
//
//  Created by student on 16/3/25.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "Video.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *list;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //1.获取数据
    NSString *urlStr =@"http://192.168.13.31:8080/MJServer/video?method=get&type=JSON";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    
    //2.解析数据转换为OC对象
    //NSData->OC对象(NSDictionary  NSArray)
    NSError *error;
    
    NSDictionary *resultDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    NSLog(@"%@",resultDict);
    
    //3.转化成模型放入列表数组
    NSArray *videoArr = resultDict[@"videos"];
    self.list= [NSMutableArray array];
    for (NSDictionary *dict in videoArr) {
        Video *v =[Video videoWith:dict];
        [self.list addObject:v];
    }

    // 练习:
    // 1. 获取并解析无锡的天气信息:
    // http://www.weather.com.cn/adat/cityinfo/101190201.html
    
    // 2. 获取并解析今日比特币行情信息:
    //https://www.okcoin.cn/api/ticker.do
    
    // 3. 获取并解析股票信息
    // http://api.money.126.net/data/feed/100002,1000001,1000881,money.api
    // (提示:需要先转换成字符串,去掉不必要的字符串,再转换回data，再进行解析


    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    Video *v=self.list[indexPath.row];
    
    cell.textLabel.text=v.name;
    cell.detailTextLabel.text =v.url;
    return cell;
}

@end
